#include "MTableWidget.h"
#include  <QApplication>
#include <QMessageBox>
#include  <QDebug>
#include  <QList>
#include <QTableWidgetSelectionRange>
#include "finddir.h"

MTableWidget::MTableWidget(QWidget * parent )  :
QTableWidget(parent)
{

    this->setDragEnabled(true);
    this->setAcceptDrops(true);//设置QListWidget的Item项可以被拖拽
	setDragDropMode(QAbstractItemView::DragDrop);
	setDropIndicatorShown(true);//显示下拉

    //设置单击选择一行
    this->setSelectionBehavior(QAbstractItemView::SelectRows);//整行选中的方式
    //设置每行内容不可编辑
    this->setEditTriggers(QAbstractItemView::NoEditTriggers);
    //设置只能选择一行，不能选择多行
	this->setSelectionMode(QAbstractItemView::MultiSelection);
    this->setTextElideMode(Qt::ElideMiddle);
}

void MTableWidget::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
        startPos = event->pos(); //记录开始鼠标位置
    QTableWidget::mousePressEvent(event);
}

void MTableWidget::mouseMoveEvent(QMouseEvent *event)
{
    if (event->buttons() & Qt::LeftButton) {
        int distance = (event->pos() - startPos).manhattanLength();
        if (distance >= QApplication::startDragDistance()) //拖出开始
            performDrag();

    }

    QTableWidget::mouseMoveEvent(event);
}

void MTableWidget::dragEnterEvent(QDragEnterEvent *event)
{
	event->setDropAction(Qt::CopyAction);
	event->accept();
  
}

void MTableWidget::dragMoveEvent(QDragMoveEvent *event)
{
	
    MTableWidget *source =
            qobject_cast<MTableWidget *>(event->source()); //判断是不是自己
    if (source && source != this) {
		event->setDropAction(Qt::CopyAction);
        event->accept();
    }
}

void MTableWidget::dropEvent(QDropEvent *event)
{
        QList<QUrl> urlList = event->mimeData()->urls();
        int row = this->rowCount();
       
       for(int i =0;i<urlList.count();i++) //判断拖入几个文件
       {
		   this->setRowCount(row + 1 +i);
          QUrl qUrl = urlList[i];
          QTableWidgetItem *item = new QTableWidgetItem(qUrl.toString());

          this->setItem(row+i,0,item);
       }
	   event->setDropAction(Qt::CopyAction);
        event->accept();  
}

void MTableWidget::performDrag()
{
	//QUrl qUrl(item1->text());
	//urlList.append(qUrl);

	//QList<QTableWidgetSelectionRange> selectedRanges() const;
	//QList<QTableWidgetItem*> selectedItems() const;
	QList<QTableWidgetItem*> selects = this->selectedItems();
	QList<int> rows;
	for (int i = 0; i < selects.size(); i++)
	{
		int row = selects[i]->row();
		  rows.append(selects[i]->row());
	}

   // QTableWidgetItem  *item1;
   // int row = this->currentRow();
	if (rows.size()> 0) {
        QList<QUrl> urlList;
        urlList.clear();

        for(int i =0;i<rows.size();i++)
        {
			QTableWidgetItem  *item1;
           item1 = this->item(rows[i],0);
           if(item1){
           QUrl qUrl(item1->text());
           urlList.append(qUrl);
           }
        }
		
        QMimeData *mimeData = new QMimeData;
        mimeData->setUrls(urlList);

        QDrag *drag = new QDrag(this);
        drag->setMimeData(mimeData);	
        drag->setPixmap(QPixmap(":/timg.png"));
		if (drag->exec(Qt::MoveAction) == Qt::MoveAction)
		{
			
			

			char path[1024];
			Getcursorpath(path);
			qDebug() << "path" << QString::fromLocal8Bit(path);
			
			
			if (QString::fromLocal8Bit(path) == "")
			{
				//return;
			}
			else
			{
				QMessageBox::information(NULL, "Title", QString::fromLocal8Bit(path), QMessageBox::Yes, QMessageBox::Yes);
			}


			
			for (int i = 0; i < rows.size(); i++)
			{
				this->removeRow(rows[i]);//删除行		
			}
			
		}
          
    }
}


